package com.jilian.mydemo.modul.user.activity;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.base.BaseEventMsg;
import com.jilian.mydemo.common.dto.MyInfoDto;
import com.jilian.mydemo.common.vo.ServiceAreaVo;
import com.jilian.mydemo.event.RxBus;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.user.adapter.AreaAdapter;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.jilian.mydemo.utils.UrlUtils;
import com.jilian.mydemo.views.CircularImageView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 *
 */
public class MyInfoActivity extends BaseActivity implements CustomItemClickListener {
    private CircularImageView ivHead;//
    private TextView tvNickNameText;
    private TextView tvNickName;
    private TextView tvSex;
    private RecyclerView recyclerview;
    private TextView tvOk;
    private AreaAdapter areaAdapter;
    private List<ServiceAreaVo> datas;

    private LinearLayoutManager linearLayoutManager;
    private UserViewModel userViewModel;

    private TextView etHydraulicSkills;
    private TextView etAdress;
    private TextView etAdressDetail;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        userViewModel = Factoty.getViewModel(UserViewModel.class, MyInfoActivity.this);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_myinfo;
    }

    @Override
    public void initView() {
        //
        setNormalTitle("个人信息", v -> finish());
        //
        ivHead = (CircularImageView) findViewById(R.id.iv_head);
        tvNickNameText = (TextView) findViewById(R.id.tv_nick_name_text);
        tvNickName = (TextView) findViewById(R.id.tv_nick_name);
        tvSex = (TextView) findViewById(R.id.tv_sex);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        tvOk = (TextView) findViewById(R.id.tv_ok);
        etHydraulicSkills = (TextView) findViewById(R.id.et_hydraulic_skills);
        etAdress = (TextView) findViewById(R.id.et_adress);
        etAdressDetail = (TextView) findViewById(R.id.et_adress_detail);
        initAreas();
    }

    /**
     * 初始化区域
     */
    private void initAreas() {
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        areaAdapter = new AreaAdapter(this, datas, this);
        recyclerview.setAdapter(areaAdapter);
    }

    @Override
    public void initData() {
        getMyInfo();
    }

    private MyInfoDto mDto;

    /**
     * 获取个人信息数据
     */
    private void getMyInfo() {
        userViewModel.info();
        userViewModel.getInfoliveData().observe(this, new Observer<BaseDto<MyInfoDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<MyInfoDto> myInfoDtoBaseDto) {
                hideLoadingDialog();
                if (myInfoDtoBaseDto.isSuccess())
                {
                    if (myInfoDtoBaseDto.getData() != null)
                    {
                        mDto = myInfoDtoBaseDto.getData();
                        tvNickName.setText(myInfoDtoBaseDto.getData().getUserName());
                        tvSex.setText(myInfoDtoBaseDto.getData().getSex());
                        etAdress.setText(mDto.getArea());
                        etAdressDetail.setText(mDto.getContactAddress());
                        etHydraulicSkills.setText(mDto.getSkill());
                        Glide
                                .with(MyInfoActivity.this)
                                .load(UrlUtils.getUrl(myInfoDtoBaseDto.getData().getUserImg()))
                                .error(R.drawable.iv_default_head)
                                .into(ivHead);
                        List<ServiceAreaVo> list = myInfoDtoBaseDto.getData().getServiceAreas();
                        if (EmptyUtils.isNotEmpty(list)) {
                            datas.clear();
                            datas.addAll(list);
                            areaAdapter.notifyDataSetChanged();
                        }
                    } else {
                        ToastUitl.showImageToastTips(myInfoDtoBaseDto.getMsg());
                    }
                } else {
                    ToastUitl.showImageToastTips(myInfoDtoBaseDto.getMsg());
                }
            }
        });
    }

    @Override
    public void initListener() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, UpdateMyInfoActivity.class);
                intent.putExtra("mDto", mDto);
                startActivity(intent);
            }
        });
        RxBus.getInstance().toObservable().map(new Function<Object, BaseEventMsg>() {
            @Override
            public BaseEventMsg apply(Object o) throws Exception {
                return (BaseEventMsg) o;
            }
        }).subscribe(new Consumer<BaseEventMsg>() {
            @Override
            public void accept(BaseEventMsg eventMsg) throws Exception {
                if (eventMsg != null&&eventMsg.getMyInfoRefreshDto()!=null) {
                    if (eventMsg.getMyInfoRefreshDto().getCode() == 100) {
                        getMyInfo();
                    }
                }
            }
        });

    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
